package rwt.msg.internal;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import rail.mtl.ada_native.MessageTranslationLibrary.MessageTranslationLibrary_Package;

public class MethodDictionary
{
	Map<String, Method> splitMethods = new HashMap<String, Method>();
	Map<String, Method> makeMethods = new HashMap<String, Method>();
	
	public MethodDictionary()
	{
		build();
	}
	
	private void build()
	{
		Class<MessageTranslationLibrary_Package> c = MessageTranslationLibrary_Package.class;
		
		Method[] methods = c.getMethods();
		
		for (Method method : methods)
		{
			String name = method.getName();
			// System.out.println(name);
			
			String lcName = name.toLowerCase();
			
			String msgName = null;
			
			if (lcName.startsWith("split"))
			{
				msgName = name.substring(5);
				splitMethods.put(msgName, method);
			}
			else if (lcName.startsWith("make"))
			{
				msgName = name.substring(4);
				makeMethods.put(msgName, method);
			}
		}
	}
	
	public Method getSplitMethod(String methodName)
	{
		if (!makeMethods.containsKey(methodName))
			throw new RuntimeException("No such Ada MTL method: 'split" + methodName + "'");
		return splitMethods.get(methodName);
	}
	
	public Method getMakeMethod(String methodName)
	{
		if (!makeMethods.containsKey(methodName))
			throw new RuntimeException("No such Ada MTL method: 'make" + methodName + "'");
		return makeMethods.get(methodName);
	}
}
